<?php
/* KAP event Manager abstraction : appel au manager ad hoc en fonction du nom de fichier trouvé

file version 2005-05-13 10:40:48

*/

/*--------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------*/
class kap_eventManager extends kap_core_component {
/*-------------------------------------------------------------------------------------------------
																													constructeur
-------------------------------------------------------------------------------------------------*/
	function kap_eventManager(& $controller) {
		// appel du constructeur parent
		$const=get_parent_class($this);
		parent::$const($controller);
		// listage des fichiers d'evenement
		/*$regex="([^.]+).*";
		$this->eventFiles=kapFileRegexSearchPathName($regex,$this->parameters['path'],true);
		echo affvar($this->eventFiles);*/
		$this->events=$this->listEvents();
	}
/*--------------------------------------------------------------------------------------------------
																														proprietes
--------------------------------------------------------------------------------------------------*/
	var $default_parameters=array(
		'path' => 'events/',
		'managers'=>array(
			// suffix => manager-file
			// ordre de priorite inverse
			'.xmlclass.kap-event.xml'=>'eventmanager-xmlclass',
			'.classmethods.kap-event.php'=>'eventmanager-classmethods'
		)
	);
	var $managers=array(); // tableau des instances de managers
	var $eventFiles=array(); // liste des fichiers d'evenements
	var $events=array(); // liste des evenements et du gestionnaire asssocié event=>manager
/*--------------------------------------------------------------------------------------------------
																											methodes publiques
--------------------------------------------------------------------------------------------------*/
	function runEvent($name,$params=array()) {
		// analyse du nom d'evenement, recuperation du nom de base
		$ereg_name='[a-zA-Z][a-zA-Z0-9_]*';
		$ereg='^('.$ereg_name.')((::)|(->))('.$ereg_name.')$';
		if (!ereg($ereg,$name,$found)) {
			trigger_error(": incorrect event name $name",E_USER_WARNING);
			return false;
		}
		$basename=&$found[1];
		// recherche du gestionnaire associé
		if (!isset($this->events[$basename]) or !$manager=$this->events[$basename]) {
			trigger_error("kap eventmanager-autoselect: could'nt find a supported event file for event $basename",E_USER_WARNING);
			return false;
		}
		// recup du gestionnaire
		$manager=$this->getManager($manager);
		// execution

	}
/*----------------------------------------------------------------------------------------------------
																												methodes privees
----------------------------------------------------------------------------------------------------*/
	/* listage des evenements disponibles et du gestionnaire associé
	*/
	function listEvents() {
		$ereg_name='[a-zA-Z][a-zA-Z0-9_]*';
		$allevents=array();
		// pour chaque gestionnaire
		foreach ($this->parameters['managers'] as $suffix => $manager) {
			// listage des evenements correspondants
			$regex='^('.$ereg_name.')'.$suffix.'$';
			$events=kapFileRegexSearchPathName($regex,$this->parameters['path']);
			// ajout a la liste
			foreach ($events as $name => $file) {
				$allevents[$name]=$manager;
			}
		}
		echo affvar($allevents);
		return $allevents;
	}
	/* recup d'une instance unique du manager spécifié
	*/
	function getManager($manager) {
		if (!isset($this->managers[$manager]) or !is_object($this->managers[$manager])) {
			// inclusion
			$file=KAP_CORE_PATH.'classes/'.$manager.'.kap-core.class.php';
			if (!is_file($file)) {
				trigger_error("kap eventmanager-autoselect: could'nt include manager definition file ($file)",E_USER_WARNING);
			}
			include($file);
			// instanciation
			$this->managers[$manager]=& new $manager($this->C);
		}
	}
}
?>
